#!usr/lib/python
#-*-coding:utf-8-*-
"""
@author:jeffxun
@date:2013-02-22
"""
import time

class Logger:
    """ """
    LOG_FMT_DATA    =   1
    LOG_FMT_TIME    =   2
    LOG_FMT_TYPE    =   3
    LOG_FMT_DATA_TIME   =   4
    LOG_FMT_DATA_TIME_TYPE  =   5
    LOG_FMT_TIME_TYPE   =   6
    
    LOG_LEVEL_NOMAL   =   0
    LOG_LEVEL_ERROR   =   1
    LOG_LEVEL_WARNING =   2
    LOG_LEVEL_DEBUG   =   3
    def __init__(self,sfile=None,fmt=5,level=2):
        """ """
        self.__log  =   None
        self.__fmt  =   5
        self.__level=   2
        if not sfile:
            pass
        elif isinstance(sfile,str):
            self.__log = open(sfile,"w+")
        elif hasattr(sfile,"read"):
            self.__log = sfile
        elif hasattr(sfile,"debug"):
            self.__log = sfile.getlog()
        else:
            pass
    
    def debug(self,nstr):
        """ """
        if isinstance(nstr,unicode):
            nstr = unicode.encode(nstr,"utf-8")
        head = self.__getlogHead("debug")
        if self.__log :
            if self.__level >= Logger.LOG_LEVEL_DEBUG:
                print>>self.__log,"%s%s"%(head,nstr)
                self.__log.flush()
        else:
            print "%s%s"%(head,nstr)
            
    def warning(self,nstr):
        """ """
        if isinstance(nstr,unicode):
            nstr = unicode.encode(nstr,"utf-8")
        head = self.__getlogHead("warning")
        if self.__log:
            if self.__level >= Logger.LOG_LEVEL_WARNING:
                print>>self.__log,"%s%s"%(head,nstr)
                self.__log.flush()
        else:
            print "%s%s"%(head,nstr)
    
    def error(self,nstr):
        """ """
        if isinstance(nstr,unicode):
            nstr = unicode.encode(nstr,"utf-8")
        head = self.__getlogHead("error")
        if self.__log:
            if self.__level >= Logger.LOG_LEVEL_ERROR:
                print>>self.__log,"%s%s"%(head,nstr)
                self.__log.flush()
        else:
            print "%s%s"%(head,nstr)
    
    def nomal(self,nstr):
        """ """
        if isinstance(nstr,unicode):
            nstr = unicode.encode(nstr,"utf-8")
        head = self.__getlogHead("info")
        if self.__log:
            if self.__level >= Logger.LOG_LEVEL_NOMAL:
                print>>self.__log,"%s%s"%(head,nstr)
                self.__log.flush()
        else:
            print "%s%s"%(head,nstr)
        
    def setFmt(self,fmt):
        """ """
        self.__fmt = fmt
        
    def setLevel(self,level):
        """ """
        self.__level    = level        
            
    def getlog(self):
        """ """
        return self.__log
        
    def __getlogHead(self,level):
        """ """
        fmt =   ""
        if self.__fmt ==  Logger.LOG_FMT_DATA:
            fmt = "[%y-%m-%d]"
            return time.strftime(fmt,time.localtime())
        elif self.__fmt == Logger.LOG_FMT_DATA_TIME:
            fmt = "[%y-%m-%d %H:%M:%S]"
            return time.strftime(fmt,time.localtime())
        elif self.__fmt == Logger.LOG_FMT_TIME:
            fmt = "[%H:%M:%S]"
            return time.strftime(fmt,time.localtime())
        elif self.__fmt == Logger.LOG_FMT_DATA_TIME_TYPE:
            fmt = "[%y-%m-%d %H:%M:%S]"
            return "[%s]"%level+time.strftime(fmt,time.localtime())
        elif self.__fmt == Logger.LOG_FMT_TIME_TYPE:
            fmt = "[%H:%M:%S]"
            return "[%s]"%level+time.strftime(fmt,time.localtime())
        elif self.__fmt == Logger.LOG_FMT_TYPE:
            return "[%s]"%level
        else:
            return ""
        
if __name__ == "__main__":
    log = Logger("./game.log")
    log.nomal("儱騰㈣ミ每ゞ龍メ門ゞ")